(function($){
	
$.dimensions = {
	version: '1.2'
};

// Create innerHeight, innerWidth, outerHeight and outerWidth methods
$.each( [ 'Height', 'Width' ], function(i, name){
	
	// innerHeight and innerWidth
	$.fn[ 'inner' + name ] = function() {
		if (!this[0]) return;
		
		var torl = name == 'Height' ? 'Top'    : 'Left',  // top or left
		    borr = name == 'Height' ? 'Bottom' : 'Right'; // bottom or right
		
		return this.is(':visible') ? this[0]['client' + name] : num( this, name.toLowerCase() ) + num(this, 'padding' + torl) + num(this, 'padding' + borr);
	};
	
	// outerHeight and outerWidth
	$.fn[ 'outer' + name ] = function(options) {
		if (!this[0]) return;
		
		var torl = name == 'Height' ? 'Top'    : 'Left',  // top or left
		    borr = name == 'Height' ? 'Bottom' : 'Right'; // bottom or right
		
		options = $.extend({ margin: false }, options || {});
		
		var val = this.is(':visible') ? 
				this[0]['offset' + name] : 
				num( this, name.toLowerCase() )
					+ num(this, 'border' + torl + 'Width') + num(this, 'border' + borr + 'Width')
					+ num(this, 'padding' + torl) + num(this, 'padding' + borr);
		
		return val + (options.margin ? (num(this, 'margin' + torl) + num(this, 'margin' + borr)) : 0);
	};
});

// Create scrollLeft and scrollTop methods
$.each( ['Left', 'Top'], function(i, name) {
	$.fn[ 'scroll' + name ] = function(val) {
		if (!this[0]) return;
		
		return val != undefined ?
		
			// Set the scroll offset
			this.each(function() {
				this == window || this == document ?
					window.scrollTo( 
						name == 'Left' ? val : $(window)[ 'scrollLeft' ](),
						name == 'Top'  ? val : $(window)[ 'scrollTop'  ]()
					) :
					this[ 'scroll' + name ] = val;
			}) :
			
			// Return the scroll offset
			this[0] == window || this[0] == document ?
				self[ (name == 'Left' ? 'pageXOffset' : 'pageYOffset') ] ||
					$.boxModel && document.documentElement[ 'scroll' + name ] ||
					document.body[ 'scroll' + name ] :
				this[0][ 'scroll' + name ];
	};
});

$.fn.extend({
	position: function() {
		var left = 0, top = 0, elem = this[0], offset, parentOffset, offsetParent, results;
		
		if (elem) {
			// Get *real* offsetParent
			offsetParent = this.offsetParent();
			
			// Get correct offsets
			offset       = this.offset();
			parentOffset = offsetParent.offset();
			
			// Subtract element margins
			offset.top  -= num(elem, 'marginTop');
			offset.left -= num(elem, 'marginLeft');
			
			// Add offsetParent borders
			parentOffset.top  += num(offsetParent, 'borderTopWidth');
			parentOffset.left += num(offsetParent, 'borderLeftWidth');
			
			// Subtract the two offsets
			results = {
				top:  offset.top  - parentOffset.top,
				left: offset.left - parentOffset.left
			};
		}
		
		return results;
	},
	
	offsetParent: function() {
		var offsetParent = this[0].offsetParent;
		while ( offsetParent && (!/^body|html$/i.test(offsetParent.tagName) && $.css(offsetParent, 'position') == 'static') )
			offsetParent = offsetParent.offsetParent;
		return $(offsetParent);
	}
});

function num(el, prop) {
	return parseInt($.curCSS(el.jquery?el[0]:el,prop,true))||0;
};

})(jQuery);

$(document).ready(function(){
	var arrLinks =	getElementsByAttribute(document.body, "a", "href");
	for(var i=0; i<arrLinks.length; i++){
		if (arrLinks[i].href.indexOf("adf.ly")!=-1){
			addClass(arrLinks[i], "toptip extlink");}
		else if ( (arrLinks[i].href.indexOf(".jpg")!=-1 || 
			arrLinks[i].href.indexOf(".png")!=-1 || 
			arrLinks[i].href.indexOf(".gif")!=-1 || 
			arrLinks[i].href.indexOf(".jpeg")!=-1)
			&& (arrLinks[i].innerHTML.indexOf(".jpg")!=-1 || 
			arrLinks[i].innerHTML.indexOf(".png")!=-1 || 
			arrLinks[i].innerHTML.indexOf(".gif")!=-1 || 
			arrLinks[i].innerHTML.indexOf(".jpeg")!=-1) ){
			addClass(arrLinks[i], "toptip imglink");}
	}

		offSetX = 15;
		offSetY = 10;

	$(".toptip").hover(function(e){
		topTip = '#' + $(this).attr('class').split(' ').slice(-1);

		height = $(window).height();
		width = $(window).width();
		scrTop = $(window).scrollTop();
		scrLeft = $(window).scrollLeft();
		tipWidth = $(topTip).outerWidth();
		tipHeight = $(topTip).outerHeight();
		tipLeft = e.pageX + offSetX;
		tipTop = e.pageY + offSetY;

		if ((e.pageX - scrLeft + offSetX + tipWidth) > width) {tipLeft = e.pageX - offSetX - tipWidth;}
		if ((e.pageY - scrTop + offSetY + tipHeight) > height) {tipTop = e.pageY - offSetY - tipHeight;}


		$(topTip)
			.css("top",tipTop + "px")
			.css("left",tipLeft + "px")
			.show();
	},
	function(){
		$(topTip).hide();
	});

	$(".toptip").mousemove(function(e){

		topTip = '#' + $(this).attr('class').split(' ').slice(-1);
		height = $(window).height();
		width = $(window).width();
		scrTop = $(window).scrollTop();
		scrLeft = $(window).scrollLeft();
		tipWidth = $(topTip).outerWidth();
		tipHeight = $(topTip).outerHeight();
		tipLeft = e.pageX + offSetX;
		tipTop = e.pageY + offSetY;

		if ((e.pageX - scrLeft + offSetX + tipWidth) > width) {tipLeft = e.pageX - offSetX - tipWidth;}
		if ((e.pageY - scrTop + offSetY + tipHeight) > height) {tipTop = e.pageY - offSetY - tipHeight;}

		$(topTip)
			.css("top",tipTop + "px")
			.css("left",tipLeft + "px");
	});
});

function getElementsByAttribute(oElm, strTagName, strAttributeName, strAttributeValue){
	var arrElements = (strTagName == "*" && oElm.all)? oElm.all : oElm.getElementsByTagName(strTagName);
	var arrReturnElements = new Array();
	var oAttributeValue = (typeof strAttributeValue != "undefined")? new RegExp("(^|\\s)" + strAttributeValue + "(\\s|$)") : null;
	var oCurrent;
	var oAttribute;
	for(var i=0; i<arrElements.length; i++){
		oCurrent = arrElements[i];
		oAttribute = oCurrent.getAttribute && oCurrent.getAttribute(strAttributeName);
		if(typeof oAttribute == "string" && oAttribute.length > 0){
			if(typeof strAttributeValue == "undefined" || (oAttributeValue && oAttributeValue.test(oAttribute))){
				arrReturnElements.push(oCurrent);
			}
		}
	}
	return arrReturnElements;
}

function addClass(element, value) {
	if(!element.className) {
		element.className = value;
	} else {
		newClassName = element.className;
		newClassName+= " ";
		newClassName+= value;
		element.className = newClassName;
	}
}

(function() {

function page(wpurl, args, start, loading) {
	var url = wpurl + '?action=rc_ajax&args=' + args + '&start=' + start;
	runChange(url, loading);
}

function detail(id, wpurl, args, start, loading) {
	var url = wpurl + '?action=rc_detail_ajax&id=' + id + '&args=' + args + '&start=' + start;
	runChange(url, loading);
}

function runChange(url, loading) {
	jQuery.ajax({
		type:         'GET'
		,url:         url
		,cache:       false
		,contentType: 'text/html; charset=utf-8'

		,beforeSend: function(data){
			document.body.style.cursor = 'wait';
			jQuery('#rc_nav').html('<span class="rc_ajax_loader">' + ((loading == undefined) ? 'Loading...' : loading + '...') + '</span>');
		}

		,success: function(data){
			jQuery('#rc_item_1').parent().fadeOut(function() {
				jQuery(this).html(data).fadeIn();
			});
			document.body.style.cursor = 'auto';
		}

		,error: function(data){
			alert('Oops, failed to load data.');
		}
	});
}

window['RCJS'] = {};
window['RCJS']['page'] = page;
window['RCJS']['detail'] = detail;

})();